package com.trkj.base.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.ProgressBarDataFromRemote;

/**
 * Created by LiZanhong on 2015/06/27.
 */
public class AndroidSystem {
    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    /**
     * //获取版本号(内部识别号)
     * @param context
     * @return
     */
    public static int getVersionCode(Context context)
    {
        PackageInfo pi = null;
        try {
            pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi.versionCode;
    }

    /**
     * // 隐藏输入法
     *
     * @param context
     */
    public static void toggleInputMethod(Activity context) {
        ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 检查更新
     * @return
     */
    public static void checkUpgrade(final Activity context){
        ProgressBarDataFromRemote dataFromRemote =
                new ProgressBarDataFromRemote(context);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                JSONObject jsonObject = JSON.parseObject(result);

                if(!jsonObject.getBoolean("success")){
                    Toast.makeText(context, "没有网络连接", Toast.LENGTH_LONG).show();
                }else{
                    int newVersionCode = jsonObject.getIntValue("version");
                    int oldVersionCode = getVersionCode(context);

                    if(newVersionCode <= oldVersionCode){
                        Toast.makeText(context, "您安装的版本已经是最新版", Toast.LENGTH_LONG).show();
                    }else if(newVersionCode > oldVersionCode){
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle("升级提示");
                        builder.setMessage("软件当前版本为：" + getAppVersionName(context) + "，服务器最新版本为：" + jsonObject.getString("versionName") +"，要升级吗？");
                        builder.setNegativeButton("我要升级", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse(Constants.ApiUrl.TAORUIYUN_RELEASE_APK));
                                context.startActivity(intent);
                            }
                        });
                        builder.setNeutralButton("不了，谢谢", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                }
            }
        });

        dataFromRemote.execute(Constants.ApiUrl.CHECK_VERSION);
    }
}
